﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BIZ;

namespace SinhNhat.admin
{
    public partial class EditServiceOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["serviceOrderID"] != null)
                {
                    Guid id = WebUtils.ToGuid(Request.Params["serviceOrderID"]);
                    if (!id.Equals(Guid.Empty))
                    {
                        ServiceOrderBIZ serviceOrderBIZ = new ServiceOrderBIZ();
                        SERVICEORDER order = serviceOrderBIZ.getId(id);

                        if (order == null)
                        {
                            ShowError();
                        }
                        else
                        {
                            hdOrderID.Value = order.ServiceOrderID.ToString();
                            hdStatus.Value = order.Status.Value.ToString();
                            if (!order.Status.Value)
                            {
                                btnChangeStatus.Text = "Chuyển sang Đã xử lý";
                                btnChangeStatus.Visible = true;
                            }

                            hlService.NavigateUrl = order.SERVICE.Url;
                            hlService.Text = order.SERVICE.Title;
                            txtName.Text = order.CustomerName;
                            txtPhone.Text = order.CustomerPhone;
                            txtEmal.Text = order.CustomerEmail;
                            txtAddress.Text = order.CustomerAddress;
                            txtNote.Text = order.Note;
                            txtCreateDate.Text = order.CreateDate.Value.ToString();
                            txtPayment.Text = order.PAYMENT.Name;
                            if (order.PaymentID.Value.Equals(new Guid("81461F5F-D54D-4C49-83F5-72821161998A")))
                            {
                                lblInfoPayment.Text = string.Format("Bank number: {0}<br/>Bank name: {1}", order.BankNumber, order.BankName);
                            }
                        }
                    }
                    else
                    {
                        ShowError();
                    }
                }
                else
                {
                    ShowError();
                }
            }
        }

        private void ShowError()
        {
            Response.Write("<script type='text/javascript'>alert('ServiceOrderID is not correct');window.location = '/admin/AdminServiceOrder.aspx';</script>");
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            btnUpdate.Click += new EventHandler(btnUpdate_Click);
            btnChangeStatus.Click += new EventHandler(btnChangeStatus_Click);
        }

        void btnChangeStatus_Click(object sender, EventArgs e)
        {
            ServiceOrderBIZ orderBIZ = new ServiceOrderBIZ();
            orderBIZ.ChangeStatus(WebUtils.ToGuid(hdOrderID.Value), true);
            btnChangeStatus.Visible = false;

            ClientScript.RegisterStartupScript(this.GetType(), "exampleScript", "<script language ='javascript'>alert('Đơn đặt dịch vụ đã được xử lý');</script>");
        }

        void btnUpdate_Click(object sender, EventArgs e)
        {
            SERVICEORDER order = new SERVICEORDER();

            order.ServiceOrderID = WebUtils.ToGuid(hdOrderID.Value);
            order.CustomerName = txtName.Text;
            order.CustomerPhone = txtPhone.Text;
            order.CustomerEmail = txtEmal.Text;
            order.CustomerAddress = txtAddress.Text;
            order.Note = txtNote.Text;

            ServiceOrderBIZ orderBIZ = new ServiceOrderBIZ();
            orderBIZ.Edit(order);

            ClientScript.RegisterStartupScript(this.GetType(), "exampleScript", "<script language ='javascript'>alert('Đơn đặt dịch vụ đã được chỉnh sửa');</script>");
        }
    }
}